package com.project.service;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.dao.impl.UserDaoImpl;
import com.project.server.Request;
import com.project.server.Response;
import com.project.server.Servlet;
import com.project.server.WebServlet;

import java.util.List;

/**
 * 查询所有用户业务组件
 */
@WebServlet("findAll")
public class FindAllService  implements Servlet {
    private IUserDao userDao = new UserDaoImpl();
    public void service(Request request, Response response){
        List<UserBean> list = userDao.findAll();

        String str = "<table border='1' width='60%'>";
        str += "<thead><tr><th>用户名</th><th>密码</th><th>生日</th><th>工资</th><th>操作</th></tr></thead>";
        str += "<tbody>";

        for(UserBean user : list){
            String s = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href='del?id=%s'>删除</a> <a href='findById?id=%s'>修改</a></td></tr>";

            str += String.format(s,user.getName(),user.getPwd(),user.getBirthday(),user.getMoney(),user.getId(),user.getId());
        }

        str += "</tbody></table>";
        str += "<a href='add.html'>添加</a>";

        response.sendMessage(str);
    }
}
